/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package biblioteca.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import java.util.ArrayList;

import bibliOnline.db.DBConnection;
import bibliOnline.db.DBResponse;
import bibliOnline.db.RequestManager;
import bibliOnline.util.details.RequestData;
import bibliOnline.util.details.UserDetails;

public class ComunicaRichiesta extends HttpServlet 
{

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * 
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        UserDetails user = (UserDetails) request.getSession().getAttribute("user_data");
        ArrayList<RequestData> bookList = new ArrayList(1);
        DBResponse risposta;
        Gson gson = new Gson();

        // Controllo se la ricerca è semplice o avanzata, in base al parametro tipo_comando
        // 6: semplice 7: avanzata
        Integer n_comando = Integer.valueOf(request.getParameter("tipo_comando"));

        // Controllo se il comando non esiste
        if (n_comando == null) {
            risposta = new DBResponse(DBConnection.getProperty("MESSAGE_STRANGE_COMMAND"));
        } // Gestione dei comandi: la numerazione prosegue in continuità con ComunicaClienti
        else {
            switch (n_comando) {
                                 
                 case 21:
                    try {
                        String msg_risposta = RequestManager.addRequest(user, request.getParameterMap());
                        risposta = new DBResponse(msg_risposta);
                        risposta.setUsername(user.getUsername());
                    } catch (Exception ex) {
                        // Messaggio di errore 
                        risposta = new DBResponse(ex.getMessage());
                    }
                    break;
                case 16:
                    try {
                        bookList = RequestManager.lista_proposte_acquisto(user);
                        if (bookList.isEmpty()) {
                            risposta = new DBResponse(DBConnection.getProperty("MESSAGE_EMPTY_RESULT_REQUEST"));
                        } else {
                            risposta = new DBResponse();
                            risposta.InitRequest(bookList);
                        }
                    } catch (Exception ex) {
                        // Messaggio di errore 
                        risposta = new DBResponse(ex.getMessage());
                    }
                    break;        

                default:
                    // Comando errato
                    risposta = new DBResponse(DBConnection.getProperty("MESSAGE_STRANGE_COMMAND"));
            }
        }

        // Trasmetto la risposta, trasformata in Json
        try {
            out.println(gson.toJson(risposta));
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
